package com.stn.zjx.givingtreasure.fragment.bimain;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.LeftWalletItemListAdapter;
import com.stn.zjx.givingtreasure.adapter.MarketPriceListAdapter;
import com.stn.zjx.givingtreasure.adapter.TokenItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseBiFragment;
import com.stn.zjx.givingtreasure.domain.EthMarketPriceDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.EthApi;
import com.stn.zjx.givingtreasure.net.impl.EthNetImpl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class BiMarketFragment extends BaseBiFragment {


    MarketPriceListAdapter adapter;
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.twinkling_refresh)
    TwinklingRefreshLayout twinklingRefresh;
    private LinearLayoutManager lm;


    public Integer pageNow=1;
    public  Integer pageSize=10;
    private boolean isMoreLoading=false;
    private Integer visibleItemCount=0;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        if (rootView == null) {
            rootView = View.inflate(activity, R.layout.fragment_bi_market, null);
            ButterKnife.bind(this, rootView);

        }
        return rootView;
    }

    @Override
    public void initData() {
        lm = new LinearLayoutManager(activity, OrientationHelper.VERTICAL, false);
        rv.setLayoutManager(lm);
        rv.setAdapter(adapter = new MarketPriceListAdapter(activity));

        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!isMoreLoading && newState == RecyclerView.SCROLL_STATE_IDLE && visibleItemCount == adapter.dataBeanList.size() - 1 &&
                        adapter.dataBeanList.size() >= pageNow * 10) {
                    pageNow++;
                    initAppend();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = lm.findLastVisibleItemPosition();

            }
        });
        ProgressLayout headerView = new ProgressLayout(activity);
        headerView.setColorSchemeResources(R.color.quotation_font1);
        twinklingRefresh.setHeaderView(headerView);
        twinklingRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                pageNow=1;
                initRefresh();
            }
        });
        twinklingRefresh.startRefresh();

    }


    public void initRefresh(){

        int start=1+(pageNow-1)*pageSize;
        EthNetImpl
                .query_market(start, pageNow + "_query_market", new StringCallback() {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                       twinklingRefresh.finishRefreshing();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        EthMarketPriceDomain domain=new Gson().fromJson(response.body(),EthMarketPriceDomain.class);
                        if(domain.getData()!=null&&domain.getData().size()!=0){
                           adapter.refreshList(domain.getData());
                        }else{
                            adapter.clearList();
                        }
                    }

//                    @Override
//                    public void onCacheSuccess(Response<String> response) {
//                        super.onCacheSuccess(response);
//                        EthMarketPriceDomain domain=new Gson().fromJson(response.body(),EthMarketPriceDomain.class);
//                        if(domain.getData()!=null&&domain.getData().size()!=0){
//                            adapter.refreshList(domain.getData());
//                        }else{
//                            adapter.clearList();
//                        }
//                    }
                });
    }
    public void initAppend(){
        int start=1+(pageNow-1)*pageSize;
        EthNetImpl
                .query_market(start, pageNow + "_query_market", new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        EthMarketPriceDomain domain=new Gson().fromJson(response.body(),EthMarketPriceDomain.class);
                        if(domain.getData()!=null&&domain.getData().size()!=0){
                            adapter.appendList(domain.getData());
                        }
                    }
                });
    }
}
